package com.jintian.smart.kernel.core.domain;

import java.util.HashMap;
import java.util.Map;

import com.jintian.smart.kernel.core.orm.ISimpleCondition;
import org.beetl.sql.core.page.DefaultPageRequest;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

/**
 * 翻页传输对象，如果翻页参数名不通，可以继承PageDTO。
 */

@Data
@EqualsAndHashCode(callSuper = false)
@Slf4j
public class PageQuery<T, Q extends ISimpleCondition> extends DefaultPageRequest<T> {
    private static final long serialVersionUID = 1L;
    Q simpleCondition;

    public PageQuery() {
        this.setPageNo(1);
        this.setPageSize(10);
        this.setTotalRequired(true);
        this.setListRequired(true);
    }

    @Override
    @JsonIgnore
    public Object getParas() {
        return super.getParas();
    }

    // ********************兼容page属性********************/
    public Long getPage() {
        return this.getPageNo();
    }

    public void setPage(long page) {
        this.setPageNo(page);
    }

    public Integer getLimit() {
        return getPageSize();
    }

    public void setLimit(int limit) {
        this.setPageSize(limit);
    }


    // *********************************************************/
    public static PageQuery<?, ?> valueOf(Map<String, Object> request, Class<?> entityClass, Class<?> conditionClass, ObjectMapper objectMapper) {
        if (conditionClass == null) {
            log.warn("实体{}未指定查询类，将采用template查询方式", entityClass.getName());
            conditionClass = MapSimpleCondition.class;
        }
        PageQuery<?, ?> ret = objectMapper.convertValue(request, objectMapper.getTypeFactory().constructParametricType(PageQuery.class, entityClass, conditionClass));
        return ret;
    }

    public static class MapSimpleCondition extends HashMap<String, Object> implements ISimpleCondition {
        private static final long serialVersionUID = 1L;
    }
}
